<template>
  <div>
    <div v-if="details.title !== undefined">
      <!-- Header 区域 -->
      <van-nav-bar
        fixed
        title="文章详情"
        left-arrow
        @click-left="$router.back()"
      />

      <!-- 文章信息区域 -->
      <div class="article-container">
        <!-- 文章标题 -->
        <h1 class="art-title">{{ details.title }}</h1>

        <!-- 用户信息 -->
        <van-cell
          center
          :title="details.aut_name"
          :label="form(details.pubdate)"
        >
          <template #icon>
            <img :src="details.aut_photo" alt="" class="avatar" />
          </template>
          <template #default>
            <div>
              <van-button
                type="info"
                size="mini"
                v-if="details.is_followed"
                @click="follFn(true)"
                >已关注</van-button
              >
              <van-button
                icon="plus"
                type="info"
                size="mini"
                v-else
                plain
                @click="follFn(false)"
                >关注</van-button
              >
            </div>
          </template>
        </van-cell>

        <!-- 分割线 -->
        <van-divider></van-divider>

        <!-- 文章内容 -->
        <div class="art-content" v-html="details.content"></div>

        <!-- 分割线 -->
        <van-divider>End</van-divider>

        <!-- 点赞 -->
        <div class="like-box">
          <van-button
            icon="good-job"
            type="danger"
            size="small"
            v-if="details.attitude === 1"
            @click="likeFn(false)"
            >已点赞
          </van-button>
          <van-button
            icon="good-job-o"
            type="danger"
            plain
            size="small"
            v-else
            @click="likeFn(true)"
            >点赞</van-button
          >
        </div>
      </div>
      <div>
        <CommentList :coll="details.is_collected"></CommentList>
      </div>
    </div>

    <van-loading color="red" text-color="#000" v-else>加载中...</van-loading>
  </div>
</template>

<script>
import { timeAgo } from '@/utils/date.js'
import { newsDetailsAPI } from '@/api/index.js'
import {
  obtainACustomsAPI,
  focusOnAPI,
  likeAPI,
  cancelAPI
} from '@/api/index.js'
import CommentList from './CommentList.vue'
export default {
  name: 'dis',
  data() {
    return {
      details: {}
    }
  },
  async created() {
    const { data: res } = await newsDetailsAPI(this.$route.query.art_id)
    this.details = res.data
  },
  methods: {
    form: timeAgo,
    async likeFn(e) {
      if (e === true) {
        this.details.attitude = 1
        await likeAPI(this.details.art_id)
      } else {
        this.details.attitude = -1
        await cancelAPI(this.details.art_id)
      }
    },
    async follFn(e) {
      if (e === true) {
        this.details.is_followed = false
        await obtainACustomsAPI({ target: this.details.aut_id })
        console.log(this.details.aut_id)
      } else {
        this.details.is_followed = true
        await focusOnAPI({ target: this.details.aut_id })
      }
    }
  },
  components: { CommentList }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}

.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;

  /deep/ img {
    width: 100%;
  }

  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;

  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}

/deep/.van-loading {
  padding-top: 46px;
  text-align: center;
}
</style>
